package cc.vv.btpartyjob.jnimodule.server

import android.app.Activity
import android.os.Handler
import android.text.TextUtils
import cc.vv.baselibrary.util.ColorUtil
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.view.JniCancelAndConfirmDialog
import cc.vv.baselibrary.view.imgselector.ImageSelector
import cc.vv.baselibrary.view.imgselector.utils.LKPermissionUtil
import cc.vv.btpartyjob.jnimodule.R

/**
 * 操作Server
 * Created by chen.qi on 2018/9/26.
 */
object OperationServer {

    fun openPic(activity: Activity) {
        val carmaSDPermission = LKPermissionUtil.getInstance().requestCarmaSDPermission(activity)
        if (carmaSDPermission) {
            val imagTag = LKPrefUtil.getBoolean("imagTag", false)
            if (imagTag) {
                setClick(activity)
            } else {
                setDialog(activity)

            }
        }
    }

    /**
     * 去相册获取图片
     */
    private fun setClick(activity: Activity) {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(1)
                .setGridColumns(3)
                .setShowCamera(true)
                .setShowVideo(false)
                .startSelectPic(activity)
    }

    /**
     * 拦截dialog
     */
    private fun setDialog(activity: Activity) {
        val bcCancelAndConfirmDialog = JniCancelAndConfirmDialog(activity)
        //设置标题
        bcCancelAndConfirmDialog.setTitle("\"宝塔党建\"想访问你的照片")
        bcCancelAndConfirmDialog.setTitleColor(R.color.color_303030)
        bcCancelAndConfirmDialog.setTitleSize(16)
        //设置内容
        bcCancelAndConfirmDialog.setContent("是否允许访问你的相册?")
        bcCancelAndConfirmDialog.setContentColor(R.color.color_999999)
        bcCancelAndConfirmDialog.setContextSize(14f)
        //设置是否字体
        bcCancelAndConfirmDialog.confirmBtnText = "允许"
        bcCancelAndConfirmDialog.cancelBtnText = "不允许"
        bcCancelAndConfirmDialog.setEventInterface(object : JniCancelAndConfirmDialog.EventInterface {
            override fun cancelOnClick() {}

            override fun confirmOnClick() {
                setClick(activity)
                LKPrefUtil.putBoolean("imagTag", true)
            }
        })
        if (!bcCancelAndConfirmDialog!!.isShowing) {
            bcCancelAndConfirmDialog.show()
        }
    }

    /**
     * 拦截的dialog
     */
    fun operationDialog(activity: Activity, handler: Handler, title: String, content: String) {
        val bcCancelAndConfirmDialog = JniCancelAndConfirmDialog(activity)
        //设置标题
        bcCancelAndConfirmDialog.setTitle(title)
        bcCancelAndConfirmDialog.setTitleColor(ColorUtil.setBCColor(R.color.color_484A4D))
        bcCancelAndConfirmDialog.setTitleSize(16)
        //设置内容
        bcCancelAndConfirmDialog.setContent(content)
        bcCancelAndConfirmDialog.setContentColor(ColorUtil.setBCColor(R.color.color_9C9EA3))
        bcCancelAndConfirmDialog.setContextSize(14f)
        //设置是否字体
        bcCancelAndConfirmDialog.confirmBtnText = "确定"
        bcCancelAndConfirmDialog.cancelBtnText = "取消"
        bcCancelAndConfirmDialog.setEventInterface(object : JniCancelAndConfirmDialog.EventInterface {
            override fun cancelOnClick() {
            }

            override fun confirmOnClick() {
                handler.sendEmptyMessage(7)
            }
        })
        if (!bcCancelAndConfirmDialog.isShowing) {
            bcCancelAndConfirmDialog.show()
        }
    }

    /**
     * 时间转换工具(截取长度)
     */
    fun formatStr(time: String?): String? {
        return if (!TextUtils.isEmpty(time) && time!!.length > 10) {
            time.substring(0, 10)
        } else {
            time
        }
    }
}